//1.引入express
const { response } = require("express");
const express = require("express");

//2.创建应用对象
const app = express();

//3.创建路由规则
//request是对请求报文的封装
//response是对响应报文的封装
app.all("/server",(request,response)=>{
    //设置响应头 设置允许跨越
    response.setHeader("Access-Control-Allow-Origin","*");
    //设置响应体
    response.send("hello ajax all");
});

app.get("/server-get",(request,response)=>{
    //设置响应头 设置允许跨越
    response.setHeader("Access-Control-Allow-Origin","*");
    //设置响应体
    response.send("hello ajax get");
});

//延时响应
app.all("/delay",(request,response)=>{
    //设置响应头 设置允许跨越
    response.setHeader("Access-Control-Allow-Origin","*");
    response.setHeader("Access-Control-Allow-Headers","*");
    setTimeout(()=>{
        //设置响应体
        response.send("hello ajax 延时响应");
    },1000)
});

//jQuery服务
app.all("/jquery-server",(request,response)=>{
    //设置响应头 设置允许跨越
    response.setHeader("Access-Control-Allow-Origin","*");
    response.setHeader("Access-Control-Allow-Headers","*");
    //response.send("hello ajax");
    const data={name:"尚硅谷"};
    response.send(JSON.stringify(data));
});
app.post("/jquery-server2",(request,response)=>{
    //设置响应头 设置允许跨越
    response.setHeader("Access-Control-Allow-Origin","*");
    response.send("hello jQuery ajaxa");
});

//axios服务
app.all("/axios-server",(request,response)=>{
    //设置响应头 设置允许跨越
    response.setHeader("Access-Control-Allow-Origin","*");
    response.setHeader("Access-Control-Allow-Headers","*");
    //response.send("hello ajax");
    const data={name:"尚硅谷"};
    response.send(JSON.stringify(data));
});
//fetch服务
app.all("/fetch-server",(request,response)=>{
    //设置响应头 设置允许跨越
    response.setHeader("Access-Control-Allow-Origin","*");
    response.setHeader("Access-Control-Allow-Headers","*");
    //response.send("hello ajax");
    const data={name:"尚硅谷"};
    response.send(JSON.stringify(data));
});

//IE缓存
app.get("/ie",(request,response)=>{
    //设置响应头 设置允许跨越
    response.setHeader("Access-Control-Allow-Origin","*");
    //设置响应体
    response.send("hello ajax ie-4");
});

//可以接受任意类型的请求
app.all("/json-server",(request,response)=>{
    //设置响应头 设置允许跨越
    response.setHeader("Access-Control-Allow-Origin","*");
    //设置响应头 设置允许跨越
    response.setHeader("Access-Control-Allow-Headers","*");
    //响应一个数据
    const data=
    {
        name: "atguigu"
    };
    //对对象进行字符串的转换
    let str=JSON.stringify(data);
    //设置响应体
    response.send(str);
});

//4.监听端口
app.listen(8000,()=>{
    console.log("服务已经启动，8000端口监听中……");
})